﻿using _250210作业.DAL;
using _250210作业.Model;
using MVC_Project.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _250210作业.BLL
{
    public class GoodsBLL : BaseBLL<Goods>
    {
        public override void SetDal()
        {
            Dal = new GoodsDAL();
        }
        public List<Goods> GetGoodByPage(int page,int size,string name,out int count) 
        {
            var users = base.Query();
            var list = users;

            if (!string.IsNullOrWhiteSpace(name))
            {
                list = users.Where(u => u.name.Contains(name) || u.content.Contains(name));
            }

            count = list.Count();

            list = list.OrderBy(u => u.gId).Skip((page - 1) * size).Take(size);

            return list.ToList();
        }
        public bool UpdateGood(Goods good) 
        {
            var data = base.Query().Where(g => g.gId == good.gId).FirstOrDefault();
          
            //1. 修改商品信息信息表
            data.name = good.name;
            data.price = good.price;
            data.unit = good.unit;
            data.gtId = good.gtId;
            data.use_property = good.use_property;
            data.use_spec = false;
            data.content = good.content;
            data.images = good.images;
            data.sort = good.sort;


            var kdal = new GoodsPropertyKeyDAL();
            var vdal = new GoodsPropertyValueDAL();

            //2. 删除旧 values
            foreach (var key in data.GoodsPropertyKey)
            {
                vdal.Delete(key.GoodsPropertyValue.ToList());
            }

            //3. 删除旧的 keys
            var keys = data.GoodsPropertyKey;
            kdal.Delete(keys.ToList());




            //4. 添加新的key value
            kdal.Add(good.GoodsPropertyKey.ToList());

            //5. 修改数据
            Dal.Update(data);

            //6. 保存修改
            return Dal.SaveChanges();
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteGood(int id)
        {
            var data = base.Query().Where(g => g.gId == id).FirstOrDefault();

            var kdal = new GoodsPropertyKeyDAL();
            var vdal = new GoodsPropertyValueDAL();

            //1. 删除旧 的 values
            foreach (var key in data.GoodsPropertyKey)
            {
                vdal.Delete(key.GoodsPropertyValue.ToList());
            }

            //2. 删除旧的 keys
            var keys = data.GoodsPropertyKey;
            kdal.Delete(keys.ToList());


            //3. 删除商品表
            Dal.Delete(data);

            //4.保存更改
            return Dal.SaveChanges();

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteBatchGood(int[] ids)
        {

            var data = base.Query().Where(g => ids.Contains(g.gId));

            var kdal = new GoodsPropertyKeyDAL();
            var vdal = new GoodsPropertyValueDAL();

            //1. 批量删除values
            foreach (var good in data)
            {
                foreach (var key in good.GoodsPropertyKey)
                {
                    vdal.Delete(key.GoodsPropertyValue.ToList());
                }
            }

            //2. 批量删除 keys
            foreach (var good in data)
            {
                var keys = good.GoodsPropertyKey;
                kdal.Delete(keys.ToList());
            }

            //3 批量删除商品表  批量
            Dal.Delete(data.ToList());

            return Dal.SaveChanges();
        }
    }
}

